import config from '../../utils/config'
import {request} from "../../utils/request";
import formatTime  from "../../utils/util"
const app = getApp()
Page({
    data: {
        custom: wx.getMenuButtonBoundingClientRect(),
        picUrl: config.picUrl,
        hideComment: true,
        type: 2,
        title: '发现',
        page: 1,
        limit: 10,
        last_page: 1,
        loading: false,
        list: [],
        user: wx.getStorageSync('userInfo'),
        commentList: [],
        commentIndex:null,
        commentId:null,
        content:'',
        endHide:true
    },
    onLoad: function (options) {
        
    },
    onShow() {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                selected: 3
            })
        }
        this.getList()
        if(!this.data.user){
            var timer = setInterval(()=>{
                console.log(timer)
                var userInfo = wx.getStorageSync('userInfo')
                if(userInfo){
                    this.setData({user:userInfo})
                    clearInterval(timer)
                }
            },1000)
        }
    },
    changeType(e) {
        var type = e.currentTarget.dataset.type
        var obj = {1: '关注', 2: '发现', 3: '同城'}
        var historyType = this.data.type
        this.setData({
            type,
            title: obj[type],
            page: 1,
            last_page: 1,
            endHide:true
        })
        if(type == 3 && !this.data.user.county){
            this.getAddress(res=>{
                if(res.code != 200){
                    this.setData({type:historyType,title:obj[type]})
                    wx.showToast({title:"获取位置信息失败",icon:"none"})
                }else{
                    this.setData({'user.county':res.data.county})
                    this.getList()
                    this.setUserAddress(res.data)
                }
            })
        }else{
            this.getList()
        }

    },
    getList() {
        request({
            url: '/miniprogram/subcircle/getSubcircleList',
            data: {page: this.data.page, limit: this.data.limit, type: this.data.type, user_id: this.data.user.id,county:this.data.user.county}
        }).then(res => {
            if (this.data.page == 1) {
                this.setData({list: []})
            }
            this.setData({
                list: this.data.list.concat(res.data),
                last_page: res.last_page
            })
        })
    },
    getAddress(callback) {
        wx.showLoading()
        wx.getFuzzyLocation({
            type: 'wgs84',
            success: res => {
                if (res.errno != 0) {
                    wx.hideLoading()
                    wx.showToast({title: res.errMsg, icon: 'none'})
                    callback({code: 201, msg: res.errMsg})
                    return false
                }
                var getAddressUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + res.latitude + "," + res.longitude + "&key=LBJBZ-AMQ66-PHRSD-MXHZK-UFYW3-DZBYH";
                wx.request({
                    url: getAddressUrl,
                    success: rel => {
                        wx.hideLoading()
                        rel = rel.data
                        callback({
                            code: 200, msg: '', data: {
                                province: rel.result.address_component.province,
                                city: rel.result.address_component.city,
                                county: rel.result.address_component.district
                            }
                        })
                    }
                })
            },
            fail(e) {
                wx.hideLoading()
                callback({code: 201, msg: '申请拒绝'})
            }
        })
    },
    setUserAddress(obj){
        request({
            url:"/miniprogram/user/setAddress",
            data:{...obj,id:this.data.user.id},
            type:'post'
        }).then(res=>{
            wx.setStorageSync('userInfo',res.data)
            app.globalData.userInfo = res.data
            this.setData({user:res.data})
        })
    },
    toggleFans(e){
        if(this.data.loading){
            return false
        }
        this.setData({loading:true})
        var index = e.currentTarget.dataset.index,
            list = this.data.list
        if(list[index].fans.indexOf(this.data.user.id) > -1){
            list[index].fans.splice(list[index].fans.indexOf(this.data.user.id),1)
        }else{
            list[index].fans.push(this.data.user.id)
        }
        this.setData({list})
        request({
            url:"/miniprogram/user/toggleFans",
            data:{user_id:this.data.user.id,brand_id:list[index].brand_id.id},
            type:'post'
        }).then(res=>{
            this.setData({loading:false})
        })
    },
    toggleLikeSubcircle(e){
        if(this.data.loading) return false
        this.setData({loading:true})
        var index = e.currentTarget.dataset.index,
            list = this.data.list
        list[index].is_like = list[index].is_like == 1 ? 0 : 1
        list[index].likes = list[index].is_like == 1 ? list[index].likes + 1 : list[index].likes - 1
        this.setData({list})
        request({
            url:"/miniprogram/user/toggleLikeSubcircle",
            data:{subcircle_id:list[index].id,user_id:this.data.user.id},
            type:'post'
        }).then(res=>{
            this.setData({loading:false})
        })
    },
    submitComment(e){
        if(this.data.loading) return false
        var {id,index} = e.currentTarget.dataset,
            list = this.data.list,
            commentList = this.data.commentList
        this.setData({loading:true})
        request({
            url:'/miniprogram/user/addComment',
            type:'post',
            data:{subcircle_id:id,user_id:this.data.user.id,content:e.detail.value}
        }).then(res=>{
            this.setData({loading:false,content:''})
            if(res.code != 200){
                wx.showToast({title:'评论失败',icon:'none'})
                return false
            }
            res.data.avatar = this.data.user.avatar
            res.data.nickname = this.data.user.nickname
            list[index].comment.unshift(res.data)
            this.setData({list,commentList:list[index].comment})
        })
    },
    showComment(e){
        var {id,index} = e.currentTarget.dataset
        this.setData({commentList:this.data.list[index].comment,hideComment:false,commentId:id,commentIndex:index})
    },
    closeShadow(){
        this.setData({hideComment:true,commentId:null,commentIndex:null,commentList:[]})
    },
    commentLike(e){
        if(this.data.loading) return false
        this.setData({loading:true})
        var {id,index,like} = e.currentTarget.dataset,
            list = this.data.list
        list[this.data.commentIndex].comment[index].likes = like == 1 ? list[this.data.commentIndex].comment[index].likes - 1 : list[this.data.commentIndex].comment[index].likes + 1
        list[this.data.commentIndex].comment[index].is_like = like == 1 ? 0 : 1
        request({
            url:"/miniprogram/user/commentLike",
            type:'post',
            data:{subcircle_comment_id:id,user_id:this.data.user.id}
        }).then(res=>{
            this.setData({loading:false})
            if(res.code != 200){
                wx.showToast({title:res.msg,icon:'none'})
                return false
            }
            this.setData({list,commentList:list[this.data.commentIndex].comment})
        })

    },
    onReachBottom(){
        if(!this.data.endHide) return false
        if(this.data.page >= this.data.last_page){
            this.setData({endHide:false})
            return false
        }
        this.setData({page:this.data.page+1})
        this.getList()
    },
    prevImg(e){
        var {i1,i2} = e.currentTarget.dataset,
            photos = []
        for(let v of this.data.list[i1].banner){
            photos.push(config.picUrl + v)
        }
        wx.previewImage({
            urls:photos,
            current:photos[parseInt(i2)]
        })
    }
});